package o;

import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.Fragment.SavedState;
import java.util.ArrayList;
import o.kp.c;

public abstract class kc extends PagerAdapter {
    private Fragment ʼ;
    private ArrayList<SavedState> ˊ;
    private final int ˋ;
    private final FragmentManager ˎ;
    private FragmentTransaction ˏ;
    private ArrayList<Fragment> ॱ;

    @NonNull
    public abstract Fragment ˏ(int i);

    @Deprecated
    public kc(@NonNull FragmentManager juVar) {
        this(juVar, 0);
    }

    public kc(@NonNull FragmentManager juVar, int i) {
        this.ˏ = null;
        this.ˊ = new ArrayList();
        this.ॱ = new ArrayList();
        this.ʼ = null;
        this.ˎ = juVar;
        this.ˋ = i;
    }

    public void startUpdate(@NonNull ViewGroup viewGroup) {
        if (viewGroup.getId() == -1) {
            throw new IllegalStateException("ViewPager with adapter " + this + " requires a view id");
        }
    }

    @NonNull
    public Object instantiateItem(@NonNull ViewGroup viewGroup, int i) {
        if (this.ॱ.size() > i) {
            Fragment fragment = (Fragment) this.ॱ.get(i);
            if (fragment != null) {
                return fragment;
            }
        }
        if (this.ˏ == null) {
            this.ˏ = this.ˎ.ˊ();
        }
        Fragment ˏ = ˏ(i);
        if (this.ˊ.size() > i) {
            SavedState savedState = (SavedState) this.ˊ.get(i);
            if (savedState != null) {
                ˏ.ˎ(savedState);
            }
        }
        while (this.ॱ.size() <= i) {
            this.ॱ.add(null);
        }
        ˏ.ˊ(false);
        if (this.ˋ == 0) {
            ˏ.a_(false);
        }
        this.ॱ.set(i, ˏ);
        this.ˏ.ˋ(viewGroup.getId(), ˏ);
        if (this.ˋ == 1) {
            this.ˏ.ˏ(ˏ, c.STARTED);
        }
        return ˏ;
    }

    public void destroyItem(@NonNull ViewGroup viewGroup, int i, @NonNull Object obj) {
        Fragment fragment = (Fragment) obj;
        if (this.ˏ == null) {
            this.ˏ = this.ˎ.ˊ();
        }
        while (this.ˊ.size() <= i) {
            this.ˊ.add(null);
        }
        this.ˊ.set(i, fragment.ʾ() ? this.ˎ.ˊ(fragment) : null);
        this.ॱ.set(i, null);
        this.ˏ.ˊ(fragment);
        if (fragment == this.ʼ) {
            this.ʼ = null;
        }
    }

    public void setPrimaryItem(@NonNull ViewGroup viewGroup, int i, @NonNull Object obj) {
        Fragment fragment = (Fragment) obj;
        if (fragment != this.ʼ) {
            if (this.ʼ != null) {
                this.ʼ.ˊ(false);
                if (this.ˋ == 1) {
                    if (this.ˏ == null) {
                        this.ˏ = this.ˎ.ˊ();
                    }
                    this.ˏ.ˏ(this.ʼ, c.STARTED);
                } else {
                    this.ʼ.a_(false);
                }
            }
            fragment.ˊ(true);
            if (this.ˋ == 1) {
                if (this.ˏ == null) {
                    this.ˏ = this.ˎ.ˊ();
                }
                this.ˏ.ˏ(fragment, c.RESUMED);
            } else {
                fragment.a_(true);
            }
            this.ʼ = fragment;
        }
    }

    public void finishUpdate(@NonNull ViewGroup viewGroup) {
        if (this.ˏ != null) {
            this.ˏ.ˊ();
            this.ˏ = null;
        }
    }

    public boolean isViewFromObject(@NonNull View view, @NonNull Object obj) {
        return ((Fragment) obj).getView() == view;
    }

    @Nullable
    public Parcelable saveState() {
        Bundle bundle = null;
        if (this.ˊ.size() > 0) {
            bundle = new Bundle();
            Parcelable[] parcelableArr = new SavedState[this.ˊ.size()];
            this.ˊ.toArray(parcelableArr);
            bundle.putParcelableArray("states", parcelableArr);
        }
        Parcelable parcelable = bundle;
        for (int i = 0; i < this.ॱ.size(); i++) {
            Fragment fragment = (Fragment) this.ॱ.get(i);
            if (fragment != null && fragment.ʾ()) {
                if (parcelable == null) {
                    parcelable = new Bundle();
                }
                this.ˎ.ˋ(parcelable, "f" + i, fragment);
            }
        }
        return parcelable;
    }

    public void restoreState(@Nullable Parcelable parcelable, @Nullable ClassLoader classLoader) {
        if (parcelable != null) {
            Bundle bundle = (Bundle) parcelable;
            bundle.setClassLoader(classLoader);
            Parcelable[] parcelableArray = bundle.getParcelableArray("states");
            this.ˊ.clear();
            this.ॱ.clear();
            if (parcelableArray != null) {
                for (Parcelable parcelable2 : parcelableArray) {
                    this.ˊ.add((SavedState) parcelable2);
                }
            }
            for (String str : bundle.keySet()) {
                if (str.startsWith("f")) {
                    int parseInt = Integer.parseInt(str.substring(1));
                    Fragment ˊ = this.ˎ.ˊ(bundle, str);
                    if (ˊ != null) {
                        while (this.ॱ.size() <= parseInt) {
                            this.ॱ.add(null);
                        }
                        ˊ.ˊ(false);
                        this.ॱ.set(parseInt, ˊ);
                    } else {
                        Log.w("FragmentStatePagerAdapt", "Bad fragment at key " + str);
                    }
                }
            }
        }
    }
}
